package com.zjhb.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zjhb.common.dao.BaseDao;
import com.zjhb.common.enumutil.Area;
import com.zjhb.common.enumutil.Verify;
import com.zjhb.common.util.DateUtil;
import com.zjhb.common.util.ResourceUtil;
import com.zjhb.common.util.ResponseUtil;
import com.zjhb.common.util.StringMyUtil;
import com.zjhb.dto.Chart;
import com.zjhb.dto.ChartDto;
import com.zjhb.entity.DepartmentEntity;
import com.zjhb.entity.KeepApplyEntity;

import param.ChartParam;

@RestController
@RequestMapping("/data")
public class DataController {
	@Autowired
	BaseDao baseDao;

	// 已完成维修
	@PostMapping("/chart")
	public JSONObject chart(ChartParam param) {
		ChartDto dto = new ChartDto();
		dto.setFinish(finishi());
		dto.setRefuse(refuse());
		dto.setKeeping(keeping());
		dto.setVerify(verify());
		if (StringMyUtil.isNotEmpty(param.getAreaId())) {
			dto.setAreaName(Area.parse(param.getAreaId()).getValue());
		}
		if (StringMyUtil.isNotEmpty(param.getDepartId())) {
			DepartmentEntity dep = baseDao.getById(DepartmentEntity.class, param.getDepartId());
			if (dep != null) {
				dto.setDepartName(dep.getName());
			}
		}
		rows(dto, param);
		return ResponseUtil.getMap(ResponseUtil.TRUE_CODE, "", dto);
	}

	public void rows(ChartDto dto, ChartParam param) {
		Criteria crt = baseDao.getCriteria(KeepApplyEntity.class);
		List<Chart> charts = new ArrayList<>();

		if (StringMyUtil.isNotEmpty(param.getStatus())) {
			if (param.getStatus().equals("1")) {
				crt.add(Restrictions.eq("status", Verify.WaitKeep.getKey()));
			} else if (param.getStatus().equals("2")) {
				crt.add(Restrictions.eq("status", Verify.Finish.getKey()));
			} else if (param.getStatus().equals("0")) {
				crt.add(Restrictions.eq("status", Verify.WaitVerify.getKey()));
			}
		}
		if (StringMyUtil.isNotEmpty(param.getAreaId())) {
			crt.add(Restrictions.eq("areaId", param.getAreaId()));
		}
		if (StringMyUtil.isNotEmpty(param.getDepartId())) {
			crt.add(Restrictions.eq("departId", param.getDepartId()));
		}
		if(!ResourceUtil.isAdminOrLeader()){
			crt.add(Restrictions.eq("departId",ResourceUtil.getCurrentUser().getDepartId()));
		}
		crt.add(Restrictions.between("createTime", ResourceUtil.getTime(DateUtil.subDays(new Date(), param.getDay())),
				ResourceUtil.getTime(DateUtil.endOfDate(new Date()))));
		List<KeepApplyEntity> list = crt.list();
		dto.setTotal(list.size());
		if (list.size() > 0) {
			for (int i = param.getDay(); i >= 0; i--) {
				String cureentDate = ResourceUtil.getNowYnd(i);
				Chart chart = new Chart();
				int j = 0;
				for (KeepApplyEntity keep : list) {
					if (keep.getCreateTime().substring(0, 10).equals(cureentDate)) {
						j++;
					}
				}
				chart.setTime(cureentDate);
				chart.setCount(j);
				charts.add(chart);
			}
		}
		dto.setRows(charts);
	}

	public Long finishi() {

		return data(Verify.Finish.getKey());
	}

	public Long refuse() {
		Criteria crt = baseDao.getCriteria(KeepApplyEntity.class);
		crt.add(Restrictions.or(Restrictions.eq("status", Verify.RefuseKeep.getKey()),
				Restrictions.eq("status", Verify.RefuseVerify.getKey())));
		if(!ResourceUtil.isAdminOrLeader()){
			crt.add(Restrictions.eq("departId",ResourceUtil.getCurrentUser().getDepartId()));
		}
		crt.add(Restrictions.between("createTime", ResourceUtil.getTime(DateUtil.subDays(new Date(), 30)),
				ResourceUtil.getTime(DateUtil.endOfDate(new Date()))));
		Long totalNum = (Long) crt.setProjection(Projections.rowCount()).uniqueResult();
		return totalNum;
	}

	public Long verify() {

		return data(Verify.WaitVerify.getKey());
	}

	public Long keeping() {
		Criteria crt = baseDao.getCriteria(KeepApplyEntity.class);
		crt.add(Restrictions.eq("status", Verify.WaitKeep.getKey()));
		if(!ResourceUtil.isAdminOrLeader()){
			crt.add(Restrictions.eq("departId",ResourceUtil.getCurrentUser().getDepartId()));
		}
		crt.add(Restrictions.between("createTime", ResourceUtil.getTime(DateUtil.subDays(new Date(), 30)),
				ResourceUtil.getTime(DateUtil.endOfDate(new Date()))));
		Long totalNum = (Long) crt.setProjection(Projections.rowCount()).uniqueResult();
		return totalNum;
	}

	private Long data(String status) {
		Criteria crt = baseDao.getCriteria(KeepApplyEntity.class);
		crt.add(Restrictions.eq("status", status));
		if(!ResourceUtil.isAdminOrLeader()){
			crt.add(Restrictions.eq("departId",ResourceUtil.getCurrentUser().getDepartId()));
		}
		crt.add(Restrictions.between("createTime", ResourceUtil.getTime(DateUtil.subDays(new Date(),30)),
				ResourceUtil.getTime(DateUtil.endOfDate(new Date()))));
		Long totalNum = (Long) crt.setProjection(Projections.rowCount()).uniqueResult();
		return totalNum;

	}
}
